<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<script>
    let a = new Promise((resolve,reject) => {
        resolve('1')
    }).then(res=>{
        console.log('eeee',a,res)
    })
    console.log(a)

    let arr =[
        {id: 1, name: '部门1', pid: 0},
        {id: 2, name: '部门2', pid: 1},
        {id: 3, name: '部门3', pid: 1},
        {id: 4, name: '部门4', pid: 3},
        {id: 5, name: '部门5', pid: 4},
    ];
    for(let b of arr){
        console.log(b)
    }
    function batt(arr,pid,res=[]){
        for(let item of arr){
            if(item.pid == pid){
                let newItem = {...item,children:[]}
                res.push(newItem)
                batt(arr,newItem.id,newItem.children)
            }
        }
        return res
    }
    let res = batt(arr,1)
    console.log(res)
    function battleArr(arr){
     // 1.0 先搞结构
        arr.forEach((item,index)=>{
            item.children = []
            item.isfather =false
            item.isChild = false
            arr.forEach((option,num)=>{
                console.log(index,num)
                // 2.0 判断谁是father 谁是child
                if(item.id = option.pid){
                    item.isfather = true;
                    item.children.push(option)
                    option.isChild = true;
                }

            })
        })
        console.log(arr)

    }
    //battleArr(arr)
</script>
</body>
</html>
